<?php

/**
 * 客服接口
 * @author ben <ben@spicatech.cn>  Created on 2015-7-31
 */
class CustomService {

    const BASE_API_URL = 'https://api.weixin.qq.com';

    protected $accessToken;

    public function __construct($accessToken) {
        $this->accessToken = $accessToken;
    }

    /**
     * 发送客服消息
     * @param array $data
     */
    protected function sendMessage(array $data) {
        $url = self::BASE_API_URL . '/cgi-bin/message/custom/send?access_token=' . $this->accessToken;
        $http = new HTTPClient();
        $http->postText($url, json_encode($data));
    }

    /**
     * 发送文本消息
     * @param type $to
     * @param type $msg
     */
    public function sendTextMessage($to, $msg) {
        $data = array(
            'touser' => $to,
            'msgtype' => 'text',
            'text' => array(
                'content' => $msg
            )
        );
        $this->sendMessage($data);
    }

}
